﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsHookLib;

namespace BackLib
{
    public class Hook
    {
        public event ToggleHandler Toggle;
        public delegate void ToggleHandler(bool Enabled, string Key);

        public event ScreenCapHandler ScreenCap;
        public delegate void ScreenCapHandler();

        KeyboardHook ManagerKeys = new WindowsHookLib.KeyboardHook();
        MouseHook ManagerMouse = new WindowsHookLib.MouseHook();

        bool alreadyPressed = false;
        bool muteToggle = true;
        public bool settingKey = false;

        Settings settings;

        public Hook(Settings settings)
        {
            this.settings = settings;

            ManagerMouse.MouseDown += new EventHandler<WindowsHookLib.MouseEventArgs>(ManagerMouse_MouseDown);
            ManagerMouse.MouseUp += new EventHandler<WindowsHookLib.MouseEventArgs>(ManagerMouse_MouseUp);
            ManagerKeys.KeyDown += new EventHandler<WindowsHookLib.KeyBoardEventArgs>(ManagerKeys_KeyDown);
            ManagerKeys.KeyUp += new EventHandler<WindowsHookLib.KeyBoardEventArgs>(ManagerKeys_KeyUp);

            ManagerMouse.InstallHook();
            ManagerKeys.InstallHook();
        }

        void ManagerMouse_MouseDown(object sender, WindowsHookLib.MouseEventArgs e)
        {
            ToggleDown("Mouse" + e.Button.ToString(), null);
        }
        void ManagerMouse_MouseUp(object sender, WindowsHookLib.MouseEventArgs e)
        {
            ToggleUp("Mouse" + e.Button.ToString(), null);
        }

        void ManagerKeys_KeyDown(object sender, WindowsHookLib.KeyBoardEventArgs e)
        {
            ToggleDown(e.KeyCode.ToString(), e);
        }
        void ManagerKeys_KeyUp(object sender, WindowsHookLib.KeyBoardEventArgs e)
        {
            ToggleUp(e.KeyCode.ToString(), e);
        }

        private void ToggleDown(string value, System.Windows.Forms.KeyEventArgs e)
        {
            if (!alreadyPressed)
            {
                if (settingKey)
                {
                    settingKey = false;
                    Toggle(muteToggle, value);
                }
                else if (value == settings.keySettings.KeyPTT)
                {
                        alreadyPressed = true;
                        if (Toggle != null) Toggle(true, null);
                        try
                        {
                            if (settings.keySettings.HandlePTTKey && e != null) e.Handled = true;
                        }
                        catch { }
                }
                else if (value == settings.keySettings.KeyMTT)
                {
                    alreadyPressed = true;
                    if (Toggle != null)
                    {
                        muteToggle = !muteToggle;
                        Toggle(muteToggle, null);
                    }
                    try
                    {
                        if (settings.keySettings.HandleMTTKey && e != null) e.Handled = true;
                    }
                    catch { }
                }
                else if (value == settings.keySettings.KeyPTC)
                {
                    alreadyPressed = true;
                    if (ScreenCap != null)
                        ScreenCap();
                    try
                    {
                        if (settings.keySettings.HandlePTCKey && e != null) e.Handled = true;
                    }
                    catch { }
                }
            }
        }

        private void ToggleUp(string value, System.Windows.Forms.KeyEventArgs e)
        {
            if (value == settings.keySettings.KeyPTT)
            {
                alreadyPressed = false;
                if (Toggle != null)
                {
                    Toggle(false, null);
                    muteToggle = false;
                }
                if (settings.keySettings.HandlePTTKey && e != null) 
                    e.Handled = true;
            }
            else if (value == settings.keySettings.KeyMTT)
            {
                alreadyPressed = false;
                if (settings.keySettings.HandleMTTKey && e != null) 
                    e.Handled = true;
            }
            else if (value == settings.keySettings.KeyPTC)
            {
                alreadyPressed = false;
                if (settings.keySettings.HandlePTCKey && e != null) 
                    e.Handled = true;
            }
        }
    }
}
